package com.bmft.grid_view11;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bmft.R;

public class MyGridViewAdapter extends BaseAdapter {
    /**
     * item数量
     */
    private int itemsCount;
    /**
     * context
     */
    private Context context;
    /**
     * layout inflater 布局填充器.
     */
    private LayoutInflater layoutInflater;

    public MyGridViewAdapter(Context context, int itemsCount) {
        this.itemsCount = itemsCount;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemsCount;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * 这里对应 layout下的 layout_grid_item_holder.xml
     */
    static class LayoutGridHolder {
        public ImageView imageView;
        public TextView textView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutGridHolder holder = null;
        //如果convertView 为空
        if (convertView == null) {
            //填充view
            convertView = layoutInflater.inflate(R.layout.layout_grid_item_holder, null);
            holder = new LayoutGridHolder();
            holder.imageView = convertView.findViewById(R.id.grid_view_image_view_01);
            holder.textView = convertView.findViewById(R.id.grid_view_text_view_title_02);
            convertView.setTag(holder);
        } else {
            convertView.setTag(convertView.getTag());
        }
        return convertView;
    }
}
